Parameters m.Servico, m.convenio, m.precoch

Private ltalk, areant, valch, preco, matser

if set('Talk') = 'ON'
   set talk off
   ltalk = .t.
  else
   ltalk = .f.
endif

m.areant = select()

Select 0
Use Convenio Again Alias ConvTmp
    set order to tag Codigo
    
Seek m.Convenio

m.ValCH = ConvTmp.Valor

Use Servicos Again Alias ServTmp
    set order to tag Codigo
    
Seek m.Servico

m.Servico   = ServTmp.Codigo
m.Preco     = ServTmp.Preco 
m.matser    = ServTmp.MatSer
m.TipoPreco = ServTmp.TipoPreco

Seek left( m.Servico, 2) + '00000'

m.GrupoServ = ServTmp.Codigo

Seek Left( m.Servico, 4) + '000'

m.SubGrServ = ServTmp.Codigo

Use ServExc again Alias ServExtmp
    set order to tag ServConv
    
if seek( m.GrupoServ + str(m.Convenio,3) ) && Excessao do Grupo
   m.ValCH     = ServExtmp.Preco
endif

if seek( m.SubGrServ + str(m.Convenio,3) ) && Excessao do SubGrupo
   m.ValCH     = ServExtmp.Preco
endif

if seek( m.Servico + str(m.Convenio,3) )   && Excessao do Servico
   m.TipoPreco = ServExtmp.TipoPreco
   m.preco = ServExtmp.Preco
endif
use

if m.TipoPreco != 2 and m.MatSer = 'S'
   * m.Preco = m.Preco
  else 
   m.Preco = round(m.preco / iif( empty(m.Precoch),m.ValCh,m.PrecoCH), 3)
endif

Select (m.Areant)

if ltalk
   set talk on
endif

return (m.preco)
